Objective-C 中 .m 文件中的 @interface

前言:博主为一枚因为工作需要正在艰难地新学 IOS 开发的程序媛,对于 Objective-C 还不甚了解,所以博文内容可能不是那么严谨,如有童鞋发现不妥之处,还望告知博主,万分感谢!

问题描述

博主今早在看一份 Objective-C 的代码时,在 SignInViewController.m 文件中看到如下代码片段:

#import "SignInViewController.h"

@interface SignInViewController ()
@property (weak, nonatomic) IBOutlet UITextField *userNameTextField;
@property (weak, nonatomic) IBOutlet UITextField *userPassTextField;
@property (weak, nonatomic) IBOutlet UIButton *signInBtn;
@property (strong, nonatomic) NSString *userName;
@property (strong, nonatomic) NSString *userPass;
@end

然后博主在仿照写代码时,一不留神将 @interface SignInViewController () 写成了 @interface SignInViewController : NSOject,然后 XCode 给出了错误提示,后面回查才发现自己把代码写错了,并且还并不了解 .m 文件中的这个 @interface SignInViewController () 是什么意思,所以就查了一波资料。

问题解析

查阅资料后发现,该处涉及到 Objective-C 中分类和扩展的知识点。

知识点分析

分类 Category

官方解释:

A category allows you to add methods to an existing class
—even to one for which you do not have the source. 
Categories are a powerful feature that allows you to 
extend the functionality of existing classes without subclassing. 
Using categories, you can also distribute the implementation 
of your own classes among several files. 

Adding Methods to Classes

You can add methods to a class by declaring them in an interface file
under a category name and defining them in an implementation file 
under the same name. 
The category name indicates that the methods are additions to a class declared elsewhere, 
not a new class. 
You cannot, however, use a category to add additional instance variables to a class.

The methods the category adds become part of the class type. 

Category methods can do anything that methods defined in the class proper can do. 
At runtime, there’s no difference. 
The methods the category adds to the class are inherited by all the class’s subclasses, 
just like other methods.

The declaration of a category interface looks very much like a class interface declaration—
except the category name is listed within parentheses after the class name 
and the superclass isn’t mentioned. 
Unless its methods don’t access any instance variables of the class,
 the category must import the interface file for the class it extends:

#import "ClassName.h"
@interface ClassName ( CategoryName )
// method declarations
@end

Note that a category can’t declare additional instance variables for the class;
it includes only methods. 
However, all instance variables within the scope of the class 
are also within the scope of the category. 
That includes all instance variables declared by the class, 
even ones declared @private.

There’s no limit to the number of categories that you can add to a class, 
but each category name must be different, 
and each should declare and define a different set of methods.

分类允许开发者为一个已有的类添加新功能,即便开发者没有该类的源码也可以如此使用。分类给予了开发者一个扩展已有类功能,而无需继承该类的强大特性,提供了不同于继承的便利。通过分类为类添加的方法就自然而然成为该类的一部分,类似于该类原来已有的方法,所有继承自该类的子类均自动拥有这些方法。

注意:

  1. 可以通过分类为已有类添加新功能,而不能通过分类添加新的实例变量
  2. 除非在分类中实现的方法均无需访问该类中的实例变量,否则在分类中必须导入类的 interface 文件;
  3. 导入了类的 interface 文件后,该类中定义的所有实例变量均自动被分类所拥有,即便是 @private 定义的实例变量,分类也可以访问;
  4. 可以为一个类添加任意数量的分类,只需要保证每个分类名称及定义在分类中的方法不同即可。

分类的使用:

使用步骤:
第一步:创建一个在指定分类名下带有接口的新文件,在该文件中添加想要添加的新功能;
第二步:在同名 implementation 文件中实现这些功能

#import <Foundation/Foundation.h>  
  
/** NSString 表示将要添加分类的类名称,该类必须是已存在的。  
  * Test 为分类名称  
  * testString 为新添加的方法
  */  

@interface NSString (Test)  
-(NSString*) testString;  
@end  
  
@implementation NSString (Test)    
-(NSString*) testString  
{  
    // 方法实现
    NSString str = @"test string";
    return str;  
}  
@end  

扩展 Extension

官方解释:

Class extensions are like anonymous categories,
except that the methods they declare must be implemented
in the main @implementation block for the corresponding class. 
Using the Clang/LLVM 2.0 compiler, you can also declare properties 
and instance variables in a class extension.

A common use for class extensions is to redeclare property that 
is publicly declared as read-only privately as readwrite:

@interface MyClass : NSObject
@property (retain, readonly) float value;
@end
 
// Private extension, typically hidden in the main implementation file.
@interface MyClass ()
@property (retain, readwrite) float value;
@end
Notice that (in contrast to a category) no name is given in the parentheses
in the second @interface block.

It is also generally common for a class to have a publicly declared API and 
to then have additional methods declared privately for use solely by the class 
or the framework within which the class resides. Class extensions allow you 
to declare additional required methods for a class in locations other than 
within the primary class @interface block.

大意为:扩展和匿名分类很像,不过和匿名分类还是有很大的区别,扩展中可以添加实例变量,扩展中定义的方法必须在相应类的 @implementation 代码块中实现。从 Xcode 4 之后就推荐在自定义类的 .m 文件中使用扩展,这样就能保证良好的代码封装性,避免把私有接口暴露给外面。

扩展的惯用法:

  1. 重新将一个在外部 interface 中声明为 readonly 的变量声明为 readwrite 类型,既便于在当前类中读写该变量,又能够避免外部修改;
  2. 在外部 interface 中声明公有 API,而在扩展中声明私有方法,可以避免将私有接口暴露于外部。

扩展的使用步骤:
第一步:创建 MyClass.h 文件,在 @interface MyClass 中声明可供外部调用的公有 API;
第二步:创建 MyClass.m 文件,在 @interface MyClass(){} 扩展中声明所需的实例变量和方法;
第三步:在 @implementation 代码块中实现在 MyClass.h 文件和 @interface MyClass(){} 扩展中声明的方法。

// 如下代码位于 MyClass.h 文件中
@interface MyClass : NSObject
- (float)value;
@end
 
// 如下代码位于 MyClass.m 文件中
@interface MyClass () {  // 扩展
    float value;
}
- (void)setValue:(float)newValue;
@end
 
// 实现
@implementation MyClass
- (float)value {
    return value;
}
- (void)setValue:(float)newValue {
    value = newValue;
}
@end

分类与扩展的区别

  1. 分类只能为已有类添加新功能,不能在分类中添加新的实例变量;而扩展不仅可以添加新功能,还能添加实例变量;
  2. 通过分类为已有类添加新功能时,需要在类名后面的小括号中指定分类名,同时可以通过指定多个分类名为已有类添加这多个分类中的功能;而使用扩展时,类名后的小括号中不添加任何东西;
  3. 扩展中声明的方法必须在相应类的 @implementation 代码块中实现,而分类则没有此要求,也就是说分类与类之间是独立的,不与特定的类相关,而扩展是与特定的类相关的,所以在扩展中声明的代码必须在相关类的 @implementation 主代码块中实现
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,290评论 4 363
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,399评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,021评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,034评论 0 207
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,412评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,651评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,902评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,605评论 0 199
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,339评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,586评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,076评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,400评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,060评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,083评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,851评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,685评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,595评论 2 270

推荐阅读更多精彩内容